package com.china08.yxyapi.api.manage.stu.mystudy.huixue.studyrecord;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyQuestionBankResource;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrainQuesMistake;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainQuesMistakeRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;
import com.china08.yxyapi.service.mg.PlanExerciseService;

/**
 * 学习记录 查看错题
 * 
 */
@CrossOrigin
@RestController
public class HuiXueStudyRecoredErrorQuesController extends YxyApiBaseController {
    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;
    @Autowired
    private PlanExerciseService planExerciseService;

    @Autowired
    private YxyHuixuePlanKnowTrainQuesMistakeRepository yxyHuixuePlanKnowTrainQuesMistakeRepository;
    /*@Autowired
    private MongoTemplate mongoTemplate;*/
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/studyrecored/errorques/list", method = RequestMethod.POST)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);

        YxyHuixuePlan yxyHuixuePlan = yxyHuixuePlanRepository.findById(reqModel.planId);
        // 开始时间
        Integer beginDate = yxyHuixuePlan.getBeginDate();
        String beginDateString = null;
        if (beginDate != null && beginDate != 0) {
            beginDateString = String.valueOf(beginDate);
            beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                    + beginDateString.substring(6);
        } else {
            beginDateString = "";
        }
        // 结束时间
        Integer endDate = yxyHuixuePlan.getEndDate();
        String endDateString = null;
        if (endDate != null && endDate != 0) {
            endDateString = String.valueOf(endDate);
            endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                    + endDateString.substring(6);
        } else {
            endDateString = "";
        }
        model.beginDateString = beginDateString;
        model.endDateString = endDateString;

        /*Page<YxyHuixuePlanKnowTrainQues> yxyHuixuePlanKnowTrainQuesLst = yxyHuixuePlanKnowTrainQuesRepository
                .findByStudentIdAndPlanIdAndKnowIdAndPassed(reqModel.studentId, reqModel.planId, reqModel.knowId, 0,
                        pageable);*/
        Page<YxyHuixuePlanKnowTrainQuesMistake> yxyHuixuePlanKnowTrainQuesMistakesPage = yxyHuixuePlanKnowTrainQuesMistakeRepository.findByPlanIdAndKnowId(reqModel.planId, reqModel.knowId,pageable);
        /*DBObject dbObject = new BasicDBObject("student_id", studentId); // 检索条件
        subjectNameLst1 = mongoTemplate.getCollection("yxy.tea.homework.task.report").distinct("subject_name", // 返回字段
                dbObject);*/
        List<QuesModel> modelLst = new ArrayList<QuesModel>();
        QuesModel quesModel = null;
        ElasticYxyQuestionBankResource elasticYxyQuestionBankResource = null;
        for (YxyHuixuePlanKnowTrainQuesMistake yxyHuixuePlanKnowTrainQuesMistake : yxyHuixuePlanKnowTrainQuesMistakesPage) {
            elasticYxyQuestionBankResource = planExerciseService.getExerciseById(yxyHuixuePlanKnowTrainQuesMistake.getQuesId());
            quesModel = new QuesModel();
            if (elasticYxyQuestionBankResource != null) {
                quesModel.quesType = elasticYxyQuestionBankResource.getQuesType();
                quesModel.countError = yxyHuixuePlanKnowTrainQuesMistake.getMistakeCount();// 每道题错误的次数
                quesModel.quesBody = elasticYxyQuestionBankResource.getQues();
                quesModel.quesId = elasticYxyQuestionBankResource.getId();
            }
            modelLst.add(quesModel);
        }
        
        
        /*List<String> questIdSet = new ArrayList<String>();// 获取当前student的当前计划下的当前知识点所对应的所有错题id
        for (YxyHuixuePlanKnowTrainQues yxyHuixuePlanKnowTrainQues : yxyHuixuePlanKnowTrainQuesLst) {
            questIdSet.add(yxyHuixuePlanKnowTrainQues.getQuesId());
        }*/
        // 现已经获取到所有试题的id，下一步应该通过试题id查询试题实体类，并且带有分页传到前端 ???
//        List<YxyHuixuePlanKnowTrainQues> yxyHuixuePlanKnowTrainQuesLst2 = null;

        /*for (String questId : questIdSet) {
            elasticYxyQuestionBankResource = planExerciseService.getExerciseById(questId);
            quesModel = new QuesModel();
            if (elasticYxyQuestionBankResource != null) {
                quesModel.quesType = elasticYxyQuestionBankResource.getQuesType();
                yxyHuixuePlanKnowTrainQuesLst2 = yxyHuixuePlanKnowTrainQuesRepository
                        .findDistinctYxyHuixuePlanKnowTrainQuesByStudentIdAndPlanIdAndKnowIdAndQuesIdAndPassed(
                                reqModel.studentId, reqModel.planId, reqModel.knowId, questId, 0);
                quesModel.countError = yxyHuixuePlanKnowTrainQuesLst2.size();// 每道题错误的次数
                // quesModel.myAnswer =
                // yxyHuixuePlanKnowTrainQuesLst2.get(yxyHuixuePlanKnowTrainQuesLst2.size()-1).getAnswerOfMy();
                // quesModel.rightAnswer =
                // yxyHuixuePlanKnowTrainQuesLst2.get(yxyHuixuePlanKnowTrainQuesLst2.size()-1).getAnswerOfQues();
                quesModel.quesBody = elasticYxyQuestionBankResource.getQues();
                quesModel.quesId = elasticYxyQuestionBankResource.getId();
            } else {
                // quesModel.quesType = "";
                // yxyHuixuePlanKnowTrainQuesLst2 =
                // yxyHuixuePlanKnowTrainQuesRepository.findDistinctYxyHuixuePlanKnowTrainQuesByStudentIdAndPlanIdAndKnowIdAndQuesIdAndPassed(reqModel.studentId,
                // reqModel.planId, reqModel.knowId,questId,0);
                // if (yxyHuixuePlanKnowTrainQuesLst2 != null) {
                // quesModel.countError =
                // yxyHuixuePlanKnowTrainQuesLst2.size();//每道题错误的次数
                // }else {
                // quesModel.countError = 0;
                // }
                // quesModel.myAnswer = new ArrayList<String>();
                // quesModel.rightAnswer = new ArrayList<String>();
                // quesModel.quesBody = new Object();
                // quesModel.quesId = "";
            }
            modelLst.add(quesModel);
        }*/
        PageImpl<QuesModel> pageImpl = new PageImpl<QuesModel>(modelLst, pageable,
                yxyHuixuePlanKnowTrainQuesMistakesPage.getTotalElements());
        Page<QuesModel> pageLst = pageImpl;
        model.pageLst = pageLst;
        model.knowName = reqModel.knowName;
        return model;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 学习计划的起始时间
        public String beginDateString;
        // 学习计划的结束时间
        public String endDateString;
        // 知识点名称
        public String knowName;
        // 试题
        public Page<QuesModel> pageLst;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 计划id
        public String planId;
        // 学生id
        public String studentId;
        // 知识点id
        public String knowId;
        // 知识点名称
        public String knowName;
    }

    protected class QuesModel {
        // 试题id
        public String quesId;
        // 试题类型
        public String quesType;
        // 错误几次
        public Integer countError;
        // 自己的答案
        public List<String> myAnswer;
        // 正确的答案
        public List<String> rightAnswer;
        // 题干和答案与解析
        public Object quesBody;
    }
}
